//
//  ZCMVUpdateViewController.m
//  VoiceMusic
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCMVUpdateViewController.h"
#import "ZCAFNetWork.h"
#import "ZCMVModel.h"
#import "ZCMVUpdataTbaleViewCell.h"
#import "ZCMVPlayerViewController.h"
#import "ZCMVDateViewController.h"
#import <Masonry.h>
#import <MJRefresh.h>
@interface ZCMVUpdateViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation ZCMVUpdateViewController
- (void)dealloc
{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor clearColor]];
    _dataArray = [[NSMutableArray alloc] init];
    [self creatView];
    [self electricnetwork];
    self.count = 2;
    // Do any additional setup after loading the view.
}

#pragma mark 创建视图
- (void)creatView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 49 - 50) style:UITableViewStylePlain];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.rowHeight = 300;
    [self.view addSubview:self.tableView];
}

#pragma mark 网络解析
- (void)electricnetwork
{
    [ZCAFNetWork getUrl:@"http://api.dongting.com/channel/channel/mvs?size=6&page=1" body:nil result:ZCJSON headerFile:nil success:^(id result) {
        if (result == nil) {
            return;
        }else {
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            ZCMVModel *mvModel = [[ZCMVModel alloc] init];
            [mvModel setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:mvModel];
            [self.tableView reloadData];
        }
        self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self loadNewData];
        }];
        self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self loadMoreData];
        }];
        [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"cell";
    ZCMVUpdataTbaleViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == NO) {
        cell = [[ZCMVUpdataTbaleViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    if (_dataArray.count > 0) {
        ZCMVModel * pageModel = [_dataArray objectAtIndex:indexPath.row];
        [cell getInfo:pageModel];
    }
    return cell;
}

#pragma mark 刷新加载
- (void)loadNewData
{
    
    _dataArray = [NSMutableArray array];
    [ZCAFNetWork getUrl:@"http://api.dongting.com/channel/channel/mvs?size=6&page=1" body:nil result:ZCJSON headerFile:nil success:^(id result) {
        if (result == nil) {
            return;
        }else {
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            ZCMVModel *mvModel = [[ZCMVModel alloc] init];
            [mvModel setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:mvModel];
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
        }
        }
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)loadMoreData
{
    NSString *url = [NSString stringWithFormat:@"http://api.dongting.com/channel/channel/mvs?size=6&page=%ld",(long)self.count];
    [ZCAFNetWork getUrl:url body:nil result:ZCJSON headerFile:nil success:^(id result) {
        if (result == nil) {
            return;
        }else {
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            ZCMVModel *mvModel = [[ZCMVModel alloc] init];
            [mvModel setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:mvModel];
            [self.tableView reloadData];
            
        }
        }
    } failure:^(NSError *error) {
        
    }];
    self.count = self.count + 1;
    [self.tableView.footer endRefreshing];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZCMVDateViewController *MVPlayVC = [[ZCMVDateViewController alloc] init];
    ZCMVModel * pageModel = self.dataArray[indexPath.row];
    MVPlayVC.MVurl = pageModel.mvList[0][@"url"];
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    UIView *view= (UIView *) [window.rootViewController.view viewWithTag:9999];
    view.hidden = YES;
    
    [self.navigationController pushViewController:MVPlayVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
